package main

import (
	"fmt"
	"strconv"
)

var (
	vMenuLabel []string
	vMenuFunc  []func()
)

func printMenu() {
	fmt.Println("-------------menu-------------------")
	nSize := len(vMenuLabel)
	for i := 0; i < nSize; i++ {
		fmt.Printf("[%d] %s\n", i, vMenuLabel[i])
	}
	fmt.Println("tips: m-print menu q-exit")
	fmt.Println("===================================")
}

func addToMenu(label string, f func()) {
	vMenuLabel = append(vMenuLabel, label)
	vMenuFunc = append(vMenuFunc, f)
}

func loopMenu()  {

	printMenu()
	for true {

		var strIn string
		fmt.Print("please input option:")
		_, err := fmt.Scanln(&strIn)
		if err != nil {
			fmt.Println("input err.please input again.")
			printMenu()
			continue
		}

		if strIn == "q" || strIn == "Q" {
			break
		}

		if strIn == "m" || strIn == "M" {
			printMenu()
			continue
		}

		nOption, err := strconv.Atoi(strIn)
		if err != nil || nOption < 0 || nOption >= len(vMenuFunc) {
			fmt.Println("input err. please input correct option.", nOption)
			fmt.Println("err:", err, "strIn:", strIn)
			continue
		}

		vMenuFunc[nOption]()
	}
}